/* Creation History
 * CRLOG	: CR1528
 * --------------------------------------------------------------
 * NAME   	: PasswdHistoryProcess.java
 * FUNCTION	: Control PasswdHistory Txn (for jis_s_passwd_history) 
 * BY     	: PH, HPMS
 * DATE   	: 25-JAN-2006
 *--------------------------------------------------------------
 * Modification History
 *--------------------------------------------------------------
 * 
 *--------------------------------------------------------------
*/

package admin.system.passwdhistory;

import java.sql.*;
import java.util.*;

import com.ffpc.sql.*;
import com.ffpc.util.*;

import admin.system.sysparam.*;
import admin.JisEncrypt;

public class PasswdHistoryProcess{

  public PasswdHistory createBean() {
    return new PasswdHistory();
  }

 /**
  * Insert 1 record
  *
  */
  public static int insert(Connection con, PasswdHistoryBean phisBean) 
    throws DataException
  {
	  PasswdHistory	phis	= (PasswdHistory) phisBean;
	  phis.setConnection(con);
	  return phis.insert();
  }

 /**
  * Update 1 record
  *
  */
  public static int update(Connection con, PasswdHistoryBean phisBean)
	throws DataException
  {
	  PasswdHistory	phis	= (PasswdHistory) phisBean;
	  phis.setConnection(con);
	  return phis.update();
  }

 /**
  * delete 1 record
  *
  */
  public static int delete(Connection con, PasswdHistoryBean phisBean) 
    throws DataException
  {
	  PasswdHistory	phis	= (PasswdHistory) phisBean;
	  phis.setConnection(con);
	  return phis.delete();
  }

 /**
  * find record list and store in array
  *
  */
  public static PasswdHistoryBean[] find(Connection con, PasswdHistoryBean phisBean) 
    throws DataException
  {
	  PasswdHistory	phis		= (PasswdHistory) phisBean;

	  BaseSqlCommand buc		= new BaseSqlCommand(con);
	  Vector	rows			= buc.find(phis);

	  PasswdHistoryBean[] beans	= new PasswdHistoryBean[rows.size()];
	  for( int i=0; i<rows.size(); i++ ) {
		  PasswdHistory bean	= new PasswdHistory();
		  bean.setColumns((Column[]) rows.get(i) );
		  beans[i]				= bean;
	  }
	  return beans;
  }

 /**
  * find record list and store in Vector
  *
  */
  public static Vector findv(Connection con, PasswdHistoryBean phisBean) 
    throws DataException
  {
	  PasswdHistoryBean[] beans	= find(con, phisBean);
	  Vector	vBean			= new Vector();
	  if ( beans.length == 0 )
	  {
		  return vBean;
	  }
	  else {
		  for( int bi=0; bi<beans.length; bi++ ) {
			  vBean.add(beans[bi]);
		  }
		  return vBean;
	  }
  }

 /**
  * check t_passwd exist in passwd history
  * false - t_passwd not found in passwd history, # means the current history size
  * true  - exist in history
  */
  public static boolean isExist(Connection con, String userid, String t_passwd)
	throws DataException
  {
	PasswdHistory phis	= new PasswdHistory();
	phis.setUSER_ID(userid);
	Vector phis_list	= findv(con, phis);
	JisEncrypt jis_encrypt = new JisEncrypt(con);

	if ( phis_list == null ) {
		return false;
	}
	else if ( phis_list.size() == 0 ) {
		return false;
	}
	else {
		System.err.println("isExist, phis_list.size() = " + phis_list.size() );
		for( int i=0; i<phis_list.size(); i++) {
			PasswdHistoryBean tmp_his = (PasswdHistoryBean) phis_list.elementAt(i);
			if ( jis_encrypt.checkPWD( t_passwd, tmp_his.getPASSWD() )  ) return true;
		}
	}
	return false;
  }

 /**
  * Update oldest record in password history
  * if kept records less than max_hist size, then only insert
  * else delete the oldest record and insert the new record
  */
  public static void updateOldestRecord(Connection con, String user_id, String long_passwd, String eff_date) 
	throws DataException
  {
	  int max_hist		= MaxHist(con);
	  int del_idx		= 0;	// index which phis is the oldest record in phis_list and to be deleted
	  String old_date	= "29991231 23:59:59";
  	  PasswdHistory phis = new PasswdHistory();
	  phis.setUSER_ID(user_id);

	  PasswdHistoryBean[] phis_list	= find(con, phis);

	  try
	  {
	  // remove the oldest record if num. of historical records reached max. hist. size
	  if ( phis_list.length == 0 ) {

	  }
	  else {
		if( phis_list.length >= max_hist ) {

			for( int k=0; k<phis_list.length; k++) {
				StringBuffer	sbk = new StringBuffer( phis_list[k].getEFF_DATE() );

				if( old_date.compareTo( sbk.toString() ) > 0 ) {
					old_date	= sbk.toString();
					del_idx = k;
				}
			}

			phis = (PasswdHistory) phis_list[del_idx];
			delete(con, phis);

		}
	  }
	  	
	  }
	  catch (Exception e)
	  {
		  System.err.println(" cr1528 Process error = " + e.toString());
	  }
	  // insert the new record
	  phis.setUSER_ID(user_id);
	  phis.setPASSWD(long_passwd);
	  phis.setEFF_DATE( eff_date );

	  insert(con, phis);
  }

  /**
   * get SysVar: PASSWD_HISTORY 
   * return the sysval in int format
   *
  */
  public static int MaxHist(Connection con)
    throws DataException
  {
	int max_record				= 5; //default value
    SysParamBean srtn			= SysParamProcess.createBean();

    srtn.setSys_Param_name("PASSWD_HISTORY");

    SysParamBean[] rtnlist		= SysParamProcess.find(con, srtn);
    
    if( rtnlist.length == 0 )	return max_record;

    srtn						= rtnlist[0];
	try
	{
  		max_record					= NumberFormatter.parseInt( srtn.getSys_Param_value() );		
	}
	catch (Exception e)
	{
		System.err.println(" PasswdHistory.MaxHist() error = " + e.toString() );
	}
    return max_record;
  }

}


